
require(['jquery','../modules/popup','../modules/pageSize'],function($,popup,pageSetSize){
    setRootFontSize(pageSetSize);
    var isAjax = false;
    var _yzTimer = 0;
    // 发送手机号 获取验证码
    function ajaxIphone(iphone){
        //alert(iphone);
        $.ajax({
            type: "get",
            url: "/Home/Letter/verifyMobileCode"+getFixedExtraUrl(),
            data: {
                mobile: iphone
            },
            dataType: "json",
            success: function(data) {
                if (data.stat){
                    timer(data.data);
                }else{
                    popup.alertPopup('验证码错误！');
                }
                isAjax = false;
            },
            error: function () {
                popup.alertPopup('服务器异常，请稍后重试！');
                isAjax = false;
            }
        });

    }
    // 倒计时方法
    function timer(_time) {
        var count = parseInt(_time)*60; // 默认时间
        var BtnCount = function() {
            if (count == 0) {
                clearTimeout(_yzTimer);
                $('.login_code').html('重获验证码');
                isAjax = false;
            } else {
                count--;
                $('.login_code').text(count < 10 ? ("0"+count) : count+"秒");
                _yzTimer = setTimeout(BtnCount, 1000);
            }
        };
        $('.login_code').text(count.toString()+"秒");
        count--;
        _yzTimer = setTimeout(BtnCount, 1000);
    }
    function ajaxCode(code,_mobile,controller,action,local_id){
        isAjax = true;
        $.post("/Home/Login/loginUser" + getFixedExtraUrl(), {code:code,mobile:_mobile,controller:controller,action:action,local_id:local_id},
            function(data){
                //alert(data.url);
                popup.alertPopup(data.msg);

                if (data.status){
                    setTimeout(function(){
                        window.location.href=data.url;
                    },1000);
                }else{
                    clearTimeout(_yzTimer);
                    $(".login_code").html("获取验证码");
                    $("#verification_code").val("");
                }
                isAjax = false;
            }, "json");
    }

    $(".login_code").on('click',function(){
        if(isAjax){
            return;
        }
        //var href = $(this).attr();
        var _phone = $("#user_name").val();
        //alert(_phone);
        var _myreg = _global.mbReg;
        //console.log(_phone);
        if(_phone){
            if(_myreg.test(_phone)){
                isAjax = true;
                ajaxIphone(_phone);
            }
            else {
                popup.alertPopup('请输入正确的手机号！');
                return;
            }
        }else{
            popup.alertPopup('请填写手机号码！');
            return;
        }
    });

    $('.btn-second').click(function(){
        var _yfCode = $("#verification_code").val();
        var _mobile = $("#user_name").val();
        var _url =  $("#url-login-data").val();

        var controller = $("#controller-login-data").data('val');
        var action = $("#action-login-data").data('val');
        var local_id = $("#local_id-login-data").data('val');

        // 匹配输入的验证嘛是否正确
        //console.log(_yfCode);
        if(_yfCode && !isAjax){
            ajaxCode(_yfCode,_mobile,controller,action,local_id);
        }
    });
});
